package com.example.demo.demoApi.apiHandler.dataClean;

import com.example.demo.demoApi.apiHandler.ApiMsgTypeHandler;
import com.example.demo.demoApi.apiHandler.ResponseHeader;
import com.example.demo.demoApi.msg.AbstractMsg;
import com.example.demo.entity.ExecuteInformationEntity;
import com.example.demo.exception.APICommonCode;
import com.example.demo.exception.BizException;
import com.example.demo.executor.Executor;
import com.example.demo.taskSchedule.DataProcessSchedule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DataCleanHandler implements ApiMsgTypeHandler<DataCleanMsg> {

    @Autowired
    private DataProcessSchedule dataProcessSchedule;

    @Autowired
    @Qualifier("mainTableExecutor")
    private Executor executor;

    @Override
    public AbstractMsg handle(DataCleanMsg msg) {
        DataCleanResponseMsg responseMsg = new DataCleanResponseMsg();
        ResponseHeader responseHeader = responseMsg.getHeader();
        responseHeader.setResponseId(msg.getRequestId());
        responseHeader.setMsgType(msg.getMsgType());
        try {
            log.info(msg.getClass().getName() + " handle:", msg);
            DataCleanRequestBody requestBody = msg.getBody();
            ExecuteInformationEntity executeInformation = requestBody.getExecuteInformation();
            executor.execute(executeInformation);
//            dataProcessSchedule.schedule(executeInformation);
        }catch (BizException e){
            responseHeader.setCode(e.getErrorCode().getCode()+"");
            responseHeader.setMsg(e.getErrorCode().getMsg());
        }catch (Exception e){
            responseHeader.setCode(APICommonCode.FAIL.getCode());
            responseHeader.setMsg(APICommonCode.FAIL.getCode());
        }
        return responseMsg;
    }

    @Override
    public void dataValidation(){

    }

    @Override
    public Class getHandleMsgType() {
        return DataCleanMsg.class;
    }
}
